<?php

namespace App\Http\Controllers\SystemManagement;

use App\Http\Controllers\IndexController;
use App\Models\SystemManagement\Department;
use App\Models\SystemManagement\DepartmentEmployee;
use Illuminate\Http\Request;

/**
 * Class DepartmentController
 * @package App\Http\Controllers\SystemManagement
 * @group 部门管理
 */
class DepartmentController extends IndexController
{
    /**
     * 新增部门
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam name string required 部门名称
     * @bodyParam full_name string required 部门全称
     * @bodyParam parent_id int required 部门上级ID
     * @bodyParam description string null 部门说明
     * @bodyParam memo_code string required 助记码
     * @bodyParam organization_id int required 所属组织id 外键 组织表
     *
     * @queryParam id 部门ID
     * @queryParam mark 部门编码
     * @queryParam name 部门名称
     * @queryParam parent_id 部门上级ID
     * @queryParam description 部门说明
     * @queryParam full_name 部门全称
     * @queryParam memo_code 助记码
     * @queryParam organization_id 所属组织id 外键 组织表
     *
     * @response 201 {"code":201,"msg":"created","data":"添加成功"}
     * @response 400 {"code":400,"msg":"failed","data":"添加失败"}
     */
    public function createDepart(\App\Http\Requests\System\Department $request){
        return $this->create($request,new Department());
    }

    /**
     * 部门列表 - 不带分页
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam organization_id int 所属组织id 外键 组织表
     * @bodyParam id int 部门ID
     *
     * @queryParam id 部门ID
     * @queryParam mark 部门编码
     * @queryParam name 部门名称
     * @queryParam parent_id 部门上级ID
     * @queryParam description 部门说明
     * @queryParam full_name 部门全称
     * @queryParam memo_code 助记码
     * @queryParam organization_id 所属组织id 外键 组织表
     *
     *
     */
    public function getAll(Request $request){
        $department =  Department::getAll($request);
        $data = generateTree($department);
        $id = $request->input('id','');
        $list = [];
        //循环获取部门及其下级部门
        if(is_numeric($id)) {
            foreach ($data as $key => $value) {
                if($value['id'] == $id) {
                    $list[] = $value;
                }
            }
        } else {
            $list[] = $data;
        }
        return response()->json(encodeMsg($list,200));
    }

    /**
     * 部门列表 - 带分页
     * @return \Illuminate\Http\JsonResponse
     *
     * @queryParam id 部门ID
     * @queryParam mark 部门编码
     * @queryParam name 部门名称
     * @queryParam parent_id 部门上级ID
     * @queryParam description 部门说明
     * @queryParam full_name 部门全称
     * @queryParam memo_code 助记码
     * @queryParam organization_id 所属组织
     *
     *
     */
    public function getDepart(){
        $per_page = request()->input('per_page');
        return $this->getList(new Department(),$per_page);
    }


    /**
     * 查看部门详情
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     *
     * @queryParam id 部门ID
     * @queryParam mark 部门编码
     * @queryParam name 部门名称
     * @queryParam parent_id 部门上级ID
     * @queryParam description 部门说明
     * @queryParam full_name 部门全称
     * @queryParam memo_code 助记码
     * @queryParam organization_id 所属组织
     *
     *
     */
    public function getOneDepart($id){
        return $this->show($id,new Department());
    }

    /**
     * 修改部门
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @bodyParam id int required 部门id
     * @bodyParam name string required 部门名称
     * @bodyParam parent_id int required 部门上级ID
     * @bodyParam description string null 部门说明
     * @bodyParam full_name string required 部门全称
     * @bodyParam memo_code string required 助记码
     * @bodyParam organization_id int required 所属组织
     *
     * @queryParam id 部门ID
     * @queryParam mark 部门编码
     * @queryParam name 部门名称
     * @queryParam parent_id 部门上级ID
     * @queryParam description 部门说明
     * @queryParam full_name 部门全称
     * @queryParam memo_code 助记码
     * @queryParam organization_id 所属组织
     *
     * @response 205 {"code":205,"msg":"updated","data":"修改成功"}
     * @response 400 {"code":400,"msg":"failed","data":"修改失败"}
     */
    public function updateDepart(\App\Http\Requests\System\Department $request,$id){
        return $this->update($request,$id,new Department());
    }

    /**
     * 批量删除部门
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam ids array 部门ID,批量删除+单条删除,以数组形式
     *
     * @response  204 {"code":204,"msg":"deleted","data":"删除成功"}
     * @response  400 {"code":400,"msg":"failed","data":"删除失败/数据格式错误"}
     */
    public function del(Request $request){
        return $this->delete($request,new Department());
    }

    /**
     * 批量恢复部门
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam ids array 部门ID,批量恢复+单条恢复,以数组形式
     *
     * @response  204 {"code":204,"msg":""deleted,"data":"恢复成功"}
     * @response  400 {"code":400,"msg":"failed","data":"恢复失败/数据格式错误"}
     */
    public function rec(Request $request){
        return $this->recover($request,new Department());
    }

    /**
     * 搜索
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam name string required 部门名称
     *
     * @queryParam id 部门ID
     * @queryParam mark 部门编码
     * @queryParam name 部门名称
     * @queryParam parent_id 部门上级ID
     * @queryParam description 部门说明
     * @queryParam full_name 部门全称
     * @queryParam memo_code 助记码
     * @queryParam organization_id 所属组织
     *
     * @response {"code":200,"msg":"success","data":"搜索成功"}
     */
    public function getBy(Request $request){
        return $this->getByCond($request,new Department());
    }

    /**
     * 设置部门负责人
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam department_id 部门ID
     * @bodyParam employee_ids 员工id字符串1,2,3
     *
     * @response {"code":200,"msg":"success","data":"部门负责人设置成功"}
     */
    public function setPrincipal(Request $request)
    {
        $department_id = $request->input('department_id','');
        if($request->has('employee_id')){
            $ids = DepartmentEmployee::where('department_id',$department_id)->get('id')->toArray();//取出中间表中对应部门的id
            $data = [];
            $employee_ids = explode(',',$request->get('employee_ids'));
            foreach ($employee_ids as $k=>$value){
                $tmp = [];
                $tmp['employee_id'] = $value;
                $tmp['department_id'] = $department_id;
                array_push($data,$tmp);
            }
            try{
                DepartmentEmployee::whereIn('id',$ids)->delete();
                DepartmentEmployee::insert($data);
                return response()->json(encodeMsg(['message' => '部门负责人设置成功!'],200));
            }catch (\Exception $e){
                DB::rollBack();
                return response()->json(encodeMsg(["message" => "部门负责人设置失败，".$e->getMessage()],404), 404);
            }
        }
    }

    /**
     * 设置人员数据权限
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam employee_id 员工id
     * @bodyParam department_ids 部门id字符串1,2,3
     *
     * @response {"code":200,"msg":"success","data":"部门负责人设置成功"}
     */
    public function setEmployeePrincipal(Request $request)
    {
        $employee_id = $request->input('employee_id','');
        if($request->has('employee_id')){
            $ids = DepartmentEmployee::where('employee_id',$employee_id)->get('id')->toArray();//取出中间表中对应部门的id
            $data = [];
            $employee_ids = explode(',',$request->get('department_ids'));
            foreach ($employee_ids as $k=>$value){
                $tmp = [];
                $tmp['employee_id'] = $employee_id;
                $tmp['department_id'] = $value;
                array_push($data,$tmp);
            }
            try{
                DepartmentEmployee::whereIn('id',$ids)->delete();
                DepartmentEmployee::insert($data);
                return response()->json(encodeMsg(['message' => '设置成功!'],200));
            }catch (\Exception $e){
                DB::rollBack();
                return response()->json(encodeMsg(["message" => "设置失败，".$e->getMessage()],404), 404);
            }
        }
    }
}
